package com.cskaoyan.service;

import com.cskaoyan.bean.po.MarketOrderExample;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.bean.vo.GoodsStatVO;
import com.cskaoyan.bean.vo.OrderStatVO;
import com.cskaoyan.bean.vo.UserStatVO;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/7 15:53
 * @Description:
 */
@Service
public class StatServiceImpl implements StatService {
    //统计的日期范围
    static int dateRange = 365;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    public UserStatVO userStat() {
        UserStatVO userStatVO = new UserStatVO();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");
        userStatVO.setColumns(columns);
        ArrayList<UserStatVO.RowsDTO> rows = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < dateRange; i++) {
            String date = simpleDateFormat.format(getPastDate(i));
            MarketUserExample example = new MarketUserExample();
            example.createCriteria().andAddTimeLike("%" + date + "%");
            long count = marketUserMapper.countByExample(example);
            rows.add(new UserStatVO.RowsDTO(date, (int) count));
        }
        userStatVO.setRows(rows);
        return userStatVO;
    }

    @Override
    public OrderStatVO orderStat() {
        OrderStatVO orderStatVO = new OrderStatVO();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        orderStatVO.setColumns(columns);

        ArrayList<OrderStatVO.RowsDTO> rows = new ArrayList<>();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < dateRange; i++) {
            String date = simpleDateFormat.format(getPastDate(i));
            MarketOrderExample example = new MarketOrderExample();
            example.createCriteria().andAddTimeLike("%" + date + "%");
            long count = marketOrderMapper.countByExample(example);
            Integer customers = marketOrderMapper.countCustomersOnOneday(date);
            if (customers == null) customers = 0;
            Double amount = marketOrderMapper.sumAmountOnOneday(date);
            if (amount == null) amount = 0.0;
            Double pcr = customers == 0 ? 0 : amount / customers;
            rows.add(new OrderStatVO.RowsDTO(amount, (int) count, customers, date, pcr));
        }
        orderStatVO.setRows(rows);
        return orderStatVO;
    }

    @Override
    public GoodsStatVO goodsStat() {
        GoodsStatVO goodsStatVO = new GoodsStatVO();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        goodsStatVO.setColumns(columns);
        ArrayList<GoodsStatVO.RowsDTO> rows = new ArrayList<>();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < dateRange; i++) {
            String date = simpleDateFormat.format(getPastDate(i));
            MarketOrderExample example = new MarketOrderExample();
            example.createCriteria().andAddTimeLike("%" + date + "%");
            long orders = marketOrderMapper.countByExample(example);
            Integer products = marketOrderGoodsMapper.countProductsOnOneday(date);
            Double amount = marketOrderGoodsMapper.countAmountOnOneday(date);
            rows.add(new GoodsStatVO.RowsDTO(amount, (int) orders, date, products));
        }
        goodsStatVO.setRows(rows);
        return goodsStatVO;
    }

    private static Date getPastDate(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -days);
        return cal.getTime();
    }
}
